﻿<%@ WebHandler Language="C#" Class="App_Store" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Collections.Generic;
public class App_Store : IHttpHandler, IRequiresSessionState
{

    string msg = string.Empty;
    readonly Store Store = new Store();
    public void ProcessRequest(HttpContext context)
    {
        CurrentContext = context;
        try
        {
            string action = context.Request.QueryString["action"].ToString();
            string sids = string.Empty;
            string token = string.Empty;
            string terminal = string.Empty;
            string shoptype = string.Empty;
            string floor = string.Empty;
            string keyword = string.Empty;
            string pageindex = string.Empty;
            string pagesize = string.Empty;
            string udid = string.Empty;
            
            udid = GetQuery("udid");

            switch (action.ToLower())
            {
                case "addfav":
                    sids = GetQuery("sids");
                    token = GetQuery("token");
                    AddFav(sids,token, udid);
                    break;
                case "getlist":
                    terminal = GetQuery("terminal");
                    shoptype = GetQuery("shoptype");
                    floor = GetQuery("floor");
                    keyword = GetQuery("keyword");
                    sids = GetQuery("sids");
                    pageindex = GetQuery("pageindex","1");
                    pagesize = GetQuery("pagesize", "10000");
                    sids = GetQuery("sids");
                    GetList(terminal, floor, keyword, sids,shoptype, int.Parse(pageindex), int.Parse(pagesize));
                    break;
                case "delfav":
                    sids = GetQuery("sids");
                    token = GetQuery("token");
                    DelFav(sids, token, udid);
                    break;
            }
        }
        catch (Exception err)
        {
            msg = "{\"result\":\"error\",\"msg\":\"" + err.Message + "\"}";
        }
        finally
        {
            context.Response.Write(msg);
        }
    }
    public HttpContext CurrentContext;
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


    public string GetQuery(string name)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        return res;
    }
    public string GetQuery(string name,string defaultvalue)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        if (string.IsNullOrEmpty(res)) res = defaultvalue;
        return res;
    }
    
   
    public void AddFav(string sids,string token, string udid)
    {
        if (string.IsNullOrEmpty(sids))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数不能为空\"}";
        }
        else
        {
            if (Store.ExistFav(sids, token))
                msg = "{\"result\":\"error\",\"msg\":\"店铺已经被收藏过了\"}";
            else
            {
                if (Store.AddFav(sids, token))
                {
                    msg = "{\"result\":\"success\"}";
                }
                else
                    msg = "{\"result\":\"error\",\"msg\":\"操作失败\"}";
            }
           
        }
    }

    public void DelFav(string sids,string token, string udid)
    {
        if (string.IsNullOrEmpty(sids) || string.IsNullOrEmpty(token))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数不能为空\"}";
        }
        else
        {
            if (Store.DelFav(sids,token))
            {
                msg = "{\"result\":\"success\"}";
            }
            else
                msg = "{\"result\":\"error\",\"msg\":\"操作失败\"}";

        }
    }

    public void GetList(string terminal, string floor, string keyword, string sids,string shoptype,int pageindex, int pagesize)
    {
        string sql = string.Empty;
        string errmsg = string.Empty;
        List<Gportal.GCMS.Entity.Content_Entity> list = new List<Gportal.GCMS.Entity.Content_Entity>();
        string strwhere = " 1=1 ";
        if (!string.IsNullOrEmpty(shoptype))
        {
            switch (shoptype)
            {
                case "1": strwhere += " and cata='餐饮' "; break;
                case "2": strwhere += " and cata='购物' "; break;
                case "3": strwhere += " and cata='休闲娱乐' "; break;
                case "4": strwhere += " and cata='服务设施' "; break;
            }
        }
        if (!string.IsNullOrEmpty(terminal))
            strwhere += string.Format(" and terminal like '%{0}%'", terminal);
        if (!string.IsNullOrEmpty(floor))
            strwhere += string.Format(" and floor = '{0}'", floor);
        if (!string.IsNullOrEmpty(keyword))
            strwhere += string.Format(" and (cname like  '%{0}%' or ename like '%{0}%'  or intro like '%{0}%' )", keyword);
        if (!string.IsNullOrEmpty(sids))
            strwhere += string.Format(" and Content_Entity.ContentId in ({0}) ", sids);
        int count=0;
        list=Store.GetList(strwhere,pageindex,pagesize,ref count);
        if (string.IsNullOrEmpty(errmsg))
        {
            msg = "{\"result\":\"success\",\"count\":" + count.ToString()+ ",\"data\":[";
            for (int i = 0; i < list.Count; i++)
            {
                Gportal.GCMS.Entity.Content_Entity entity = list[i];
                int catalogId = 0;
                switch (entity.GetFieldValue("cata"))// 1.美食，2.购物 ，3.休闲&娱乐，4.设施
                {
                    case "餐饮": catalogId = 1;
                        break;
                    case "购物": catalogId = 2;
                        break;
                    case "休闲娱乐": catalogId = 3;
                        break;
                    case "服务设施": catalogId = 4;
                        break;
                    default:
                        catalogId = 0;
                        break;
                }
                
                msg += "{\"sid\":" + entity.ContentId + ",";
                msg += "\"catalogId\":\"" + catalogId.ToString() + "\",";
                msg += "\"x\":\"" + entity.GetFieldValue("x") + "\",\"y\":\"" + entity.GetFieldValue("y") + "\",";
                msg += "\"cname\":\""+ entity.GetFieldValue("cname")+"\",";
                msg += "\"cata\":\""+entity.GetFieldValue("cata")+"\",";
                if (string.IsNullOrEmpty(entity.GetFieldValue("photo")))
                {
                    msg += "\"photo\":\"" + entity.GetFieldValue("photo") + "\","; 
                }
                else
                {
                    msg += "\"photo\":\"http://203.90.227.74" + entity.GetFieldValue("photo") + "\",";
                }
                msg += "\"ename\":\""+entity.GetFieldValue("ename")+"\",";
                msg += "\"intro\":\"" + JSON.CoatnvertJson(entity.GetFieldValue("intro")) + "\",";
                msg +="\"terminal\":\""+entity.GetFieldValue("terminal")+"\",";
                msg += "\"gactive\":\"" + entity.GetFieldValue("gactive") + "\",";
                msg += "\"gactivepic\":\"" + entity.GetFieldValue("gactivepic") + "\",";
                msg += "\"floor\":\""+entity.GetFieldValue("floor")+"\",";
                msg += "\"location\":\""+entity.GetFieldValue("location")+"\",";
                msg += "\"opening\":\""+ entity.GetFieldValue("opening")+"\",";
                msg +="\"tel\":\""+entity.GetFieldValue("tel")+"\",";
                msg += "\"url\":\"http://203.90.227.74/guideapp/store.html?sid=" + entity.ContentId.ToString() +"\",";//**
                if (string.IsNullOrEmpty(entity.Url))
                {
                    msg += "\"pcurl\":\"" + entity.Url + "\",";
                }
                else
                {
                    msg += "\"pcurl\":\"http://203.90.227.74" + entity.Url + "\",";
                }
                msg += "\"email\":\"" + entity.GetFieldValue("tel") + "\"}";
                if (i < list.Count-1)
                    msg += ",";
            }
            msg += "]}";

        }
        else
            msg = "{\"result\":\"error\",\"msg\":\"" + errmsg + "\"}";

       //msg = "{\"result\":\"error\",\"msg\":\"" + strwhere + "\"}";
    }
}
